//
//  YZMEntrustQueueView.m
//  YZMicroStock
//
//  Created by wangminhong on 16/4/12.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMEntrustQueueView.h"
#import "Masonry+Utils.h"
#import "YZMQuoteStyle.h"
#import "YZMStockRealModel.h"

@interface YZMEntrustQueueView()

@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) NSMutableArray *queueLabels;

@end

@implementation YZMEntrustQueueView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame: frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self initLabels];
    }
    return self;
}

- (void)initLabels
{
    self.queueLabels = [NSMutableArray array];
    NSMutableArray *colmn0Array = [NSMutableArray array];
    NSMutableArray *colmn1Array = [NSMutableArray array];
    NSMutableArray *colmn2Array = [NSMutableArray array];
    for (NSInteger i = 0; i < 30; i++) {
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize: 10];
        [self addSubview: label];
        label.text = @"--";
        [self.queueLabels addObject: label];
        if (i%3 == 0) {
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [YZMAppStyle colorForMainTextColor];
            label.font = [UIFont boldSystemFontOfSize: 10];
            [colmn0Array addObject: label];
        }
        else if (i%3 == 1)
        {
            label.textAlignment = NSTextAlignmentCenter;
            [colmn1Array addObject: label];
        }
        else
        {
            label.textColor = [YZMAppStyle colorForMainTextColor];
            label.textAlignment = NSTextAlignmentRight;
            [colmn2Array addObject: label];
        }
    }
    UIView *innerHelpView1 = [[UIView alloc] init];
    [self addSubview: innerHelpView1];
    UIView *innerHelpView2 = [[UIView alloc] init];
    [self addSubview: innerHelpView2];
    
    UILabel *firstRowHead = self.queueLabels[0];
    [firstRowHead mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.leading.equalTo(self);
    }];
    
    [innerHelpView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.leading.equalTo(firstRowHead.mas_trailing);
        make.width.equalTo(innerHelpView2);
    }];
    
    UILabel *firstRowMid = self.queueLabels[1];
    [firstRowMid mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.leading.equalTo(innerHelpView1.mas_trailing).with.offset(5);
    }];
    
    [innerHelpView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.leading.equalTo(firstRowMid.mas_trailing);
    }];
    
    UILabel *firstRowtrail = self.queueLabels[2];
    [firstRowtrail mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.leading.equalTo(innerHelpView2.mas_trailing).with.offset(5);
        make.trailing.equalTo(self);
    }];
    
    NSArray *titles = @[@"卖5", @"卖4", @"卖3", @"卖2", @"卖1",
                        @"买1", @"买2", @"买3", @"买4", @"买5"];
    for (NSInteger i = 0; i < 10; i++) {
        UILabel *leading = colmn0Array[i];
        leading.text = titles[i];
        if (i == 4) {
            UIView *viewLine = [[UIView alloc] init];
            [self addSubview:viewLine];
            [viewLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(leading.mas_bottom).offset(6.5);
                make.left.equalTo(self);
                make.right.equalTo(self);
                make.height.equalTo(@1);
            }];
//            NSInteger linWidth = 4;
//            NSInteger separetor = 3;
            NSInteger count = (100 + 3) / 7;
            UIView *ivPri;
            for (NSInteger j = 0; j < count; j++) {
                UIView *iv = [[UIView alloc] init];
                iv.backgroundColor = [YZMAppStyle colorForSeparatorLine];
                [viewLine addSubview:iv];
                [iv mas_makeConstraints:^(MASConstraintMaker *make) {
                    if (ivPri) {
                       make.left.equalTo(ivPri.mas_right).offset(3);
                        make.top.equalTo(viewLine);
                        make.height.equalTo(@1);
                        make.width.equalTo(@4);
                        
                    }else{
                        make.left.equalTo(self.mas_left).offset(3);
                        make.top.equalTo(viewLine);
                        make.height.equalTo(@1);
                        make.width.equalTo(@4);
                    }
                }];
                ivPri = iv;
            }
            
        }
        UILabel *middle = colmn1Array[i];
        UILabel *trailing = colmn2Array[i];
        [middle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(leading);
            make.bottom.equalTo(leading);
        }];
        [trailing mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(leading);
            make.bottom.equalTo(leading);
        }];
        
    }
    
    [colmn0Array mas_queueInVerticalWithGroupCount: 5 innerSpace: 13 groupSpace: 15];
    [colmn1Array mas_queueInVerticalWithGroupCount: 5 innerSpace: 13 groupSpace: 15];
    [colmn2Array mas_queueInVerticalWithGroupCount: 5 innerSpace: 13 groupSpace: 15];
}


- (void)updateData
{
    if (!self.realItem) {
        
        for (int i=0; i<5; i++) {
            
            UILabel *priceLabel = self.queueLabels[15+i*3+1];
            UILabel *amountLabel = self.queueLabels[15+i*3+2];
            priceLabel.text = @"--";
            priceLabel.textColor = [YZMQuoteStyle stableColor];
            amountLabel.text = @"--";
            amountLabel.textColor = [YZMQuoteStyle stableColor];
        }
        
        for (int i=0; i<5; i++) {
            
            UILabel *priceLabel = self.queueLabels[12-i*3+1];
            UILabel *amountLabel = self.queueLabels[12-i*3+2];
            priceLabel.text = @"--";
            priceLabel.textColor = [YZMQuoteStyle stableColor];
            amountLabel.text = @"--";
            amountLabel.textColor = [YZMQuoteStyle stableColor];
        }
        
        return;
    }
    for (NSInteger i = 0; i < 5 && i < self.realItem.buyPrices.count; i++) {
        double price = [self.realItem.buyPrices[i] doubleValue];
        NSString *priceString = [YZMQuoteStyle formatPrice: price];
        double volume = [self.realItem.buyCounts[i] doubleValue];
        NSString *amountString =  [YZMQuoteStyle formatNumberValue: volume];
        UILabel *priceLabel = self.queueLabels[15+i*3+1];
        UILabel *amountLabel = self.queueLabels[15+i*3+2];
        if (volume < DBL_EPSILON) {
            amountLabel.text = @"--";
            amountLabel.textColor = [YZMQuoteStyle stableColor];
        }
        else
        {
            amountLabel.text = amountString;
            amountLabel.textColor = [YZMAppStyle colorForMainTextColor];
        }
        if (price < DBL_EPSILON) {
            priceLabel.text = @"--";
            priceLabel.textColor = [YZMQuoteStyle stableColor];
        }
        else
        {
            priceLabel.text = priceString;
            priceLabel.textColor = [YZMQuoteStyle colorForPrice: [priceString floatValue] basic:self.realItem.lastClosePrice];
        }
        
    }
    
    for (NSInteger i = 0; i < 5 && i < self.realItem.sellPrices.count; i++) {
        
        double price = [self.realItem.sellPrices[i] doubleValue];
        NSString *priceString = [YZMQuoteStyle formatPrice: price];
        double volume = [self.realItem.sellCounts[i] doubleValue];
        NSString *amountString =  [YZMQuoteStyle formatNumberValue: volume];
        
        UILabel *priceLabel = self.queueLabels[12-i*3+1];
        UILabel *amountLabel = self.queueLabels[12-i*3+2];
        if (volume < DBL_EPSILON) {
            amountLabel.text = @"--";
            amountLabel.textColor = [YZMQuoteStyle stableColor];
        }
        else
        {
            amountLabel.text = amountString;
            amountLabel.textColor = [YZMAppStyle colorForMainTextColor];
        }
        if (price < DBL_EPSILON) {
            priceLabel.text = @"--";
            priceLabel.textColor = [YZMQuoteStyle stableColor];
        }
        else
        {
            priceLabel.text = priceString;
            priceLabel.textColor = [YZMQuoteStyle colorForPrice: [priceString floatValue] basic:self.realItem.lastClosePrice];
        }
    }
}

- (void)setRealItem:(YZMStockRealModel *)realItem
{
    _realItem = realItem;
    [self updateData];
}


- (void)clearData{
    
    _realItem = nil;
    [self updateData];
}

@end
